package org.jground.rx.ui.zk;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXClass;
import org.jground.rx.RXDataState;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXSystem;
import org.jground.rx.ui.IMainPanel;
import org.jground.rx.ui.IWindowEditAction;
import org.jground.zk.zulX.WindowX;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Caption;

public class UIEditWindow implements IMainPanel<Component> {

    //
    private static Log logger = LogFactory.getLog(UIEditWindow.class);
    //
    private UIFrame uiFrame;

    //
    protected RXClass moClass;
    private IRXObject currentMod;
    protected UIDataObjectInfoBar infoBar;

    private WindowX view;
    private UIEditPanel editPanel;
    private UIWindowEditCommandPanel windowEditCommandPanel;
    private Caption caption;

    // private UIInfoDialog noCheckOutInfoDialog;

    public UIEditWindow(UIFrame uiFrame, String moid) {
        this.uiFrame = uiFrame;
        this.moClass = RXSystem.getInstance().getRxService().getRXClass(moid);
        view = new WindowX();
        view.setBorder("normal");
        view.setClosable(true);
        //
        // COMPONENTS
        //

        caption = new Caption();
        // caption.setImage(UIStyles.getInstance().getImageUri("notfound"));
        caption.setImageContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.notfound16));

        infoBar = new UIDataObjectInfoBar();
        editPanel = new UIEditPanel(uiFrame, moClass.getMoid());
        windowEditCommandPanel = new UIWindowEditCommandPanel(uiFrame, new WindowEditAction(uiFrame, this));
        //
        // LAYOUT
        //
        view.add(infoBar);
        view.add(editPanel.getView());
        view.add(windowEditCommandPanel);
        view.addEventListener(Events.ON_CLOSE, windowEditCommandPanel);
    }

    public void setData(IRXObject mod) {
        this.currentMod = mod;
        if (mod != null && mod.checkOut()) {
            RXClass moClass = mod.getRXClass();
            // String imageUri = UIStyles.getInstance().getImageUri(mod,
            // UIStyles.S);
            // caption.setImage(imageUri);
            caption.setImageContent(UIStyles.getInstance().getImage(mod, UIStyles.S));
            view.setTitle(" " + moClass.getName());
            infoBar.setData(mod);
            editPanel.setData(mod);
            windowEditCommandPanel.setData(mod);
            view.set(caption, infoBar, editPanel.getView(), windowEditCommandPanel);
            editPanel.setFocusData(uiFrame.getPrefService().getFocusDataEdit(currentMod));
        } else {
            logger.info("checkout failed ");
            UIMessageboxUtils.showCheckOutFailed(uiFrame.getUISynopsisRendererFactory().toString(mod), mod.getCheckedOutUser());
        }
    }

    public IRXObject getData() {
        return editPanel.getData();
    }

    public void clearData() {
        editPanel.clearData();
    }

    private class WindowEditAction implements IWindowEditAction {
        private UIEditWindow editWindow;
        private UIFrame uiFrame;

        public WindowEditAction(UIFrame uiFrame, UIEditWindow editWindow) {
            this.editWindow = editWindow;
            this.uiFrame = uiFrame;
            // dispTable = new TABLE(new TR(tds));
            // dispTable = new Hbox();
        }

        public void deleteData() {
            IRXObject moDataObject = editWindow.getData();
            // List<IRXObject> subMoDataObjects =
            // editWindow.editPanel.GetDataOfSubComponents();
            if (moDataObject != null) {
                moDataObject.delete();
            }
            // foreach (IRXObject mo in subMoDataObjects) {
            // mo.Delete();
            // }
        }

        public void openConfirmDialog() {
            IRXObject moDataObject = editWindow.editPanel.getData();
            String synopsisString = uiFrame.getUISynopsisRendererFactory().toString(moDataObject, "");

            final Runnable yesRunnable = new Runnable() {

                public void run() {
                    deleteData();
                    editWindow.clearData();
                    uiFrame.closeMainPanel(editWindow);
                }
            };

            final Runnable noRunnable = new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub

                }
            };

            boolean yes = UIMessageboxUtils.showQuestionMessage("Do you really want to delete the object : '"
                    + synopsisString + "' ?", "Delete data?", yesRunnable, noRunnable);
            logger.debug("messageBox answer: " + yes);
            // if (yes) {
            // deleteData();
            // editWindow.clearData();
            // uiFrame.closeMainPanel(editWindow);
            // }
        }

        public void saveData() {
            IRXObject moDataObject = editWindow.getData();
            if (moDataObject.checkOut()) {
                if (moDataObject != null) {
                    moDataObject.save();
                }
                editWindow.setData(RXObjectService.getInstance().getById(moDataObject.getOid()));
            } else {
                //
            }
        }

        public void close() {
            IRXObject moDataObject = editWindow.editPanel.getData();
            if (moDataObject != null) {
                moDataObject.checkIn();
            }
            editWindow.clearData();
            uiFrame.closeMainPanel(editWindow);
        }

        public void revertToSaved() {
            IRXObject mod = editWindow.editPanel.getData();
            if (mod != null) {
                if (mod.getDataState() != RXDataState.NEW) {
                    long oid = mod.getOid();
                    mod = RXObjectService.getInstance().getById(oid);
                    if (mod.checkOut()) {
                        editWindow.setData(mod);
                    } else {
                        UIMessageboxUtils.showCheckOutFailed(uiFrame.getUISynopsisRendererFactory().toString(mod), mod.getCheckedOutUser());
                    }
                }
            }
        }

        public void show() {
            uiFrame.setMainPanel(editWindow, false);
        }

    }

    public void beforeClose() {
        uiFrame.getPrefService().addEdited(currentMod);
        uiFrame.getPrefService().setFocusDataEdit(currentMod, editPanel.getFocusData());
    }

    public void beforeShow() {
        // TODO Auto-generated method stub

    }

    public Component getView() {
        return view;
    }
}
